
import 'dart:convert';
import 'package:df/page/login/login.dart';
import 'package:df/page/login/login_response.dart';
import 'package:df/page/register/register.dart';
import 'package:df/page/register/register_response.dart';
import 'package:df/page/sms/code_response.dart';
import 'package:df/page/sms/sms_response.dart';
import 'package:df/page/sms/sms.dart';
import 'package:df/page/splash/show_other_login.dart';
import 'package:df/utils/logger_utils.dart';
import 'package:dio/dio.dart';
import 'package:df/page/login/view_model/login_view_model.dart';
import 'package:df/generated/l10n.dart';
import 'package:package_info_plus/package_info_plus.dart';

//用户协议
// https://sps.pheyma.com/wxkanban/UserAgreement.html
//
// 隐私协议
// https://sps.pheyma.com/wxkanban/PrivacyAgreement.html
/// dio封装：https://juejin.cn/post/7212597327579332668
/// dio封装：https://www.cnblogs.com/luoshang/p/16987781.html
/// dio封装：https://juejin.cn/post/7243383094774300730
class ApiService {
  static final int kHttpSuccess = 200;
  static final int kCodeSuccess = 0;
  static final Options options = Options(
    headers: {
      'Content-Type': 'application/json',
    },
  );

  /// SPS注册接口。
  static Future<RegisterResponse> postRegister(Register register) async {
    RegisterResponse registerResponse = RegisterResponse(resultCode: -1, resultMsg: '未知异常');
    try {
      Map<String, dynamic> registerJson = register.toJson();
      LogUtils.i("【请求】参数${registerJson}");
      Dio dio = Dio(BaseOptions(
        connectTimeout: Duration(seconds: 10),
        receiveTimeout: Duration(seconds: 10),
        sendTimeout: Duration(seconds: 10),
      ));
      var response = await dio.post(
        "https://sps.pheyma.com:5001/transferApi/api/Account/FlutterRegister",
        options: options,
        data: registerJson,
      );
      registerResponse = RegisterResponse.fromJson(response.data);
      if (response.statusCode == kHttpSuccess) {
        LogUtils.v("【响应】${response.data}");
        LogUtils.v("【响应】${registerResponse.resultMsg}");
        return registerResponse;
      } else {
        LogUtils.e("【响应】${response.statusCode}");
        return registerResponse;
      }
    } catch (e) {
      LogUtils.e("【响应】捕获到异常===============================================${e}");
      return registerResponse;
    }
  }

  /// SPS项目登录接口。{data: null, resultCode: 110, resultMsg: token不匹配, queryCount: 0, resultMsgEx: null}
  static Future<LoginResponse> postLogin(Login login) async {
    LoginResponse loginResponse = LoginResponse(resultCode: -1, resultMsg: '未知异常');
    try {
      Map<String, dynamic> loginJson = login.toJson();
      LogUtils.i("【请求】参数${loginJson}");
      Dio dio = Dio(BaseOptions(
        connectTimeout: Duration(seconds: 10),
        receiveTimeout: Duration(seconds: 10),
        sendTimeout: Duration(seconds: 10),
      ));
      var response = await dio.post(
        "https://sps.pheyma.com/wxusercenter/api/Account/LoginByAndroid",
        options: options,
        data: loginJson,
      );
      loginResponse = LoginResponse.fromJson(response.data);
      if (response.statusCode == kHttpSuccess) {
        LogUtils.v("【响应】${response.data}");
        LogUtils.v("【响应】${loginResponse.resultMsg}");
        return loginResponse;
      } else {
        LogUtils.e("【响应】${response.statusCode}");
        return loginResponse;
      }
    } catch (e) {
      LogUtils.e("【响应】捕获到异常===============================================${e}");
      return loginResponse;
    }
  }

  /// 取回短信认证码
  static Future<SmsResponse> postSendCode(Sms sms) async {
    Map<String, dynamic> smsJson = sms.toJson();
    LogUtils.i("【请求】参数${smsJson}");
    var response = await Dio().post(
      "https://sps.pheyma.com/wxusercenter/api/WeiXinSMS/SendCode",
      options: options,
      data: smsJson,
    );
    SmsResponse smsResponse = SmsResponse.fromJson(response.data);
    if (response.statusCode == kHttpSuccess) {
      LogUtils.v("【响应】${response.data}");
      LogUtils.v("【响应】${smsResponse.resultMsg}");
      return smsResponse;
    } else {
      LogUtils.e("【响应】${response.statusCode}");
      // throw Exception('${S.current.requestFailed}${response.statusCode}');
      return smsResponse;
    }
  }

  /// 是否显示其他登录(便于苹果上架)。
  static Future<ShowOtherLogin> getShowOtherLogin() async {
    ShowOtherLogin showResponse = ShowOtherLogin(resultCode: -1, resultMsg: '未知异常');
    try {
      Dio dio = Dio(BaseOptions(
        connectTimeout: Duration(seconds: 2),
        receiveTimeout: Duration(seconds: 2),
        sendTimeout: Duration(seconds: 2),
      ));
      var response = await dio.get(
        // "https://sps.pheyma.com/wxusercenter/api/Notify/GetShowOtherLogin",
        "https://sps.pheyma.com:5001/transferApi/api/MobileODS/QueryDeviceRangeVersion",
        // "https://gitee.com/hehongdan/room/raw/Dev/ShowOtherLogin.json",
        options: options,
      );
      showResponse = ShowOtherLogin.fromJson(response.data);
      if (response.statusCode == kHttpSuccess) {
        LogUtils.v("【响应】${response.data}");
        LogUtils.v("【响应】${showResponse.resultMsg}");
        return showResponse;
      } else {
        LogUtils.e("【响应】${response.statusCode}");
        return showResponse;
      }
    } catch (e) {
      LogUtils.e("【响应】捕获到异常===============================================${e}");
      return showResponse;
    }
  }

  /// 获取区号。
  static Future<CodeResponse> getCountryCode() async {
    CodeResponse codeResponse = CodeResponse(resultCode: -1, resultMsg: '未知异常');
    try {
      Dio dio = Dio(BaseOptions(
        connectTimeout: Duration(seconds: 10),
        receiveTimeout: Duration(seconds: 10),
        sendTimeout: Duration(seconds: 10),
      ));
      var response = await dio.get(
        "https://sps.pheyma.com/wxusercenter/api/Future/GetCountryAreaCode",
        options: options,
      );
      codeResponse = CodeResponse.fromJson(response.data);
      if (response.statusCode == kHttpSuccess) {
        LogUtils.v("【响应】${response.data}");
        LogUtils.v("【响应】${codeResponse.resultMsg}");
        return codeResponse;
      } else {
        LogUtils.e("【响应】${response.statusCode}");
        return codeResponse;
      }
    } catch (e) {
      LogUtils.e("【响应】捕获到异常===============================================${e}");
      return codeResponse;
    }
  }


}